package com.zhu.ttwords;

import android.app.Application;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.zhu.ttwords.bean.AppBean;

/**
 * Created by KnightYe on 2015/4/4.
 * application.
 */
public class TTWORDS extends Application {
    public static final String DATABASE_NAME = "TTWORDS.db";
    public static final String DATABASE_DIR = "TTWORDS/database";
    public static final String SP_SETTING = "setting";
    public static final String SP_VERSION_NAME = "VERSION_NAME";
    public static final String SP_VERSION_CODE = "VERSION_CODE";
    public static final String SP_STUDY_GROUP = "STUDY_GROUP";
    public static final String SP_STUDY_BOOK = "STUDY_BOOK";
    public static final String SP_CURRENT_USERID = "USERID";

    //学习模式，查询当前book中的数量为group的一组单词，当前book中还没学过的
    public static final String SQL_STUDY_INIT_STUDY = "SELECT * FROM TT_WORD WHERE wid IN ( SELECT WID FROM TT_BOOK_DIR WHERE BID = ?)" +
            " AND WID NOT IN ( SELECT WID FROM TT_REPERTORY WHERE LEVEL > 2 AND BID = ? AND UID = ?) LIMIT ?";
    //学习模式，查询当前学过的单词中，需要今天复习的所有单词。
    public static final String SQL_STUDY_INIT_REVIEW = "SELECT *  FROM TT_WORD WHERE WID IN " +
            "( SELECT WID FROM TT_REPERTORY WHERE NEXT_DATE <= ? AND BID = ? AND UID = ?)";
    //查询当前单词有学习过没有。
    public static final String SQL_EXAM_SAVE_CHECK = "SELECT * FROM TT_REPERTORY WHERE BID =? AND UID =? AND WID =?";
    //根据TEL或者EMAIL查询用户bean
    public static final String SQL_LOGIN_CHECK = "SELECT * FROM TT_USER WHERE TEL = ? OR EMAIL = ?";
    //查询主页面信息
    public static final String SQL_MAIN_TOTAL = "SELECT count(*) FROM TT_REPERTORY WHERE UID = ?";
    public static final String SQL_MAIN_TODAY = "SELECT count(*) FROM TT_REPERTORY WHERE NEXT_DATE <= ? AND UID = ?";
    public static final String SQL_MAIN_TOMORROW = "SELECT count(*) FROM TT_REPERTORY WHERE NEXT_DATE = ? AND UID = ?";


    //默认设置
    public static final int DEFAULT_STUDY_GROUP = 10;
    public static final int DEFAULT_STUDY_BOOK_ID = 0;

    //传输代码
    public static final int TRANS_SUCCESS = 200;
    public static final int TRANS_SERVER_WRONG = 500;
    public static final int TRANS_REGISTER_USED = 201;

    private static SharedPreferences sp;
    private static AppBean appSetting;
    private static int versionCode;
    private static String versionName;

    @Override
    public void onCreate() {
        try {
            PackageInfo pinfo = getPackageManager().getPackageInfo(getPackageName(),
                    PackageManager.GET_CONFIGURATIONS);
            versionName = pinfo.versionName;
            versionCode = pinfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        sp = getSharedPreferences(SP_SETTING, MODE_PRIVATE);
        appSetting = new AppBean(sp,
                sp.getInt(SP_VERSION_CODE, 0),
                sp.getString(SP_VERSION_NAME, "V1.0"),
                sp.getInt(SP_STUDY_GROUP, DEFAULT_STUDY_GROUP),
                sp.getInt(SP_STUDY_BOOK, DEFAULT_STUDY_BOOK_ID),
                sp.getString(SP_CURRENT_USERID, null));

    }

    public static AppBean getAppSetting() {
        return appSetting;
    }

    public static int getVersionCode() {
        return versionCode;
    }

    public static String getVersionName() {
        return versionName;
    }


}
